﻿using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using ToolUtil;
using zyconfig;
using zymarc;

namespace zymain
{
    public partial class FrmCxExcel : Form
    {
        private string filePath = string.Empty;
        private BindingSource BookData;
        public string uid;
        public int kjm;
        private bool flag = false;
        private DataTable tabTemp = new DataTable();
        private DataTable dt = new DataTable();
        private string marcText;
        public FrmCxExcel()
        {
            InitializeComponent();
        }

        private void FrmCxExcel_Load(object sender, EventArgs e)
        {
            
        }
        private void FrmCxExcel_Shown(object sender, EventArgs e)
        {
            tabTemp.Columns.Add("字段标识");
            tabTemp.Columns.Add("指");
            tabTemp.Columns.Add("子字段");
            dt.Columns.Add("字段标识");
            dt.Columns.Add("指");
            dt.Columns.Add("子字段");
        }

        private void excelBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Excel文件";
            ofd.FileName = "";
            //ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//为了获取特定的系统文件夹，可以使用System.Environment类的静态方法GetFolderPath()。该方法接受一个Environment.SpecialFolder枚举，其中可以定义要返回路径的哪个系统目录
            ofd.Filter = "Excel文件| *.xlsx";
            ofd.RestoreDirectory = true;//记录上次打开的目录
            ofd.Multiselect = false; //不允许选中多个文件
            ofd.ValidateNames = true;     //文件有效性验证ValidateNames，验证用户输入是否是一个有效的Windows文件名
            ofd.CheckFileExists = true;  //验证路径有效性
            ofd.CheckPathExists = true; //验证文件有效性
            string strName = string.Empty;
            if (string.IsNullOrEmpty(filePath))
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    strName = ofd.FileName;
                    filePath = strName;
                }
            }
            else
            {
                if (MessageBox.Show("确认要清除当前文件，重新加载吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    filePath = "";
                    listBox1.Items.Clear();
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        strName = ofd.FileName;
                        filePath = strName;
                    }
                }
            }
            if (strName == "")
            {
                MessageBox.Show("没有选择Excel文件！无法进行数据导入");
                return;
            }
            ExcelListBox(strName);
            if (this.listBox1.Items.Count > 0)
            {
                this.listBox1.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 读取文件工作表
        /// </summary>
        /// <param name="excelpath"></param>
        private void ExcelListBox(string excelpath)
        {
            using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(excelpath, false))
            {
                WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
                IEnumerable<Sheet> sheets = workbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
                int m = 0;
                foreach (OpenXmlElement s in sheets)
                {
                    foreach (OpenXmlAttribute attr in s.GetAttributes())
                    {
                        if (m % 3 == 0)//因为attr.Value中包含工作表名,工作表序号和ID号
                        {
                            this.listBox1.Items.Add(attr.Value);//把工作表名装到List中
                        }
                        m++;
                    }
                }
            }
        }

        /// <summary>
        /// 遍历excel工作表数据，返回DataTable
        /// </summary>
        /// <param name="excelpath"></param>
        /// <param name="listBox"></param>
        /// <returns></returns>
        private DataTable ReadFromExcel(string excelpath, string listBox)
        {
            string sExt = System.IO.Path.GetExtension(excelpath);
            if (sExt == ".xlsx" || sExt == ".xls")
            {
                DataTable excelTable = OpenXMLHelper.ExcelWorksheetToDataTable(excelpath, listBox);
                return excelTable;
            }
            else
            {
                MessageBox.Show("文件格式有误");
                return null;
            }
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            FrmWait wait = new FrmWait();
            wait.Show();
            wait.label4.Text = "";
            Application.DoEvents();
            DataTable table = new DataTable();
            table = ReadFromExcel(filePath, listBox1.SelectedItem.ToString());
            if (table.Columns.Contains("标准编码"))
            {
                table.Columns["标准编码"].ColumnName = "ISBN";
            }
            if (table.Columns.Contains("书名"))
            {
                table.Columns["书名"].ColumnName = "题名";
            }
            if (table.Columns.Contains("正题名"))
            {
                table.Columns["正题名"].ColumnName = "题名";
            }
            if (table.Rows.Count > 0)
            {
                //this.toolStripButton1.Enabled = true;
                this.BookData = new BindingSource();
                this.BookData.DataSource = table;
                this.bindingNavigator1.BindingSource = this.BookData;
                this.excelGridView.DataSource = this.BookData;
                //this.excelGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);//宽度自适应
                //this.excelGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//宽度自适应
                //this.excelGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//宽度自适应
                this.ebtBox.Items.Clear();
                for (int i = 0; i < this.excelGridView.ColumnCount; i++)
                {
                    this.ebtBox.Items.Add(this.excelGridView.Columns[i].HeaderText);
                }
                if (this.ebtBox.Items.Count > 0)
                    this.ebtBox.SelectedIndex = 0;
                for (int k = 0; k < this.excelGridView.Columns.Count; k++)
                {
                    string column = this.excelGridView.Columns[k].HeaderText;
                    switch (column)
                    {
                        case "书号":
                            this.excelGridView.Columns["书号"].HeaderText = "ISBN";
                            break;
                        case "条码":
                            this.excelGridView.Columns["条码"].HeaderText = "ISBN";
                            break;
                        case "条码号":
                            this.excelGridView.Columns["条码号"].HeaderText = "ISBN";
                            break;
                        case "标准编码":
                            this.excelGridView.Columns["标准编码"].HeaderText = "ISBN";
                            break;
                        case "isbn":
                            this.excelGridView.Columns["isbn"].HeaderText = "ISBN";
                            break;
                        case "书名":
                            this.excelGridView.Columns["书名"].HeaderText = "题名";
                            break;
                        case "图书名称":
                            this.excelGridView.Columns["图书名称"].HeaderText = "题名";
                            break;
                        case "主题名":
                            this.excelGridView.Columns["主题名"].HeaderText = "题名";
                            break;
                        case "正题名":
                            this.excelGridView.Columns["正题名"].HeaderText = "题名";
                            break;
                        case "定数":
                            this.excelGridView.Columns["定数"].HeaderText = "订数";
                            break;
                        case "价格":
                            this.excelGridView.Columns["价格"].HeaderText = "订价";
                            break;
                        case "定价":
                            this.excelGridView.Columns["定价"].HeaderText = "订价";
                            break;
                    }
                }
            }
            this.RefMarc();
            wait.Close();
            string time1 = watch.Elapsed.ToString();
            MessageBox.Show("导入：" + table.Rows.Count + "条，耗时：" + time1);
        }
        private void RefMarc()
        {
            if (this.dataGridView1.Rows.Count > 0)
            {
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.Rows.Remove(dataGridView1.Rows[i]);
                    i--;
                }
            }
            for (int i = 0; i < this.excelGridView.ColumnCount; i++)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[i].Cells[0].Value = this.excelGridView.Columns[i].HeaderText;
                string ht = this.excelGridView.Columns[i].HeaderText;
                switch (ht)
                {
                    case "ISBN":
                        dataGridView1.Rows[i].Cells[1].Value = "010";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "书号":
                        dataGridView1.Rows[i].Cells[1].Value = "010";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "标准编码":
                        dataGridView1.Rows[i].Cells[1].Value = "010";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "订价":
                        dataGridView1.Rows[i].Cells[1].Value = "010";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "d";
                        break;
                    case "单价":
                        dataGridView1.Rows[i].Cells[1].Value = "010";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "d";
                        break;
                    case "装帧":
                        dataGridView1.Rows[i].Cells[1].Value = "010";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "b";
                        break;
                    case "征订号":
                        dataGridView1.Rows[i].Cells[1].Value = "092";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "b";
                        break;
                    case "语种":
                        dataGridView1.Rows[i].Cells[1].Value = "101";
                        dataGridView1.Rows[i].Cells[2].Value = "0 ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "题名":
                        dataGridView1.Rows[i].Cells[1].Value = "200";
                        dataGridView1.Rows[i].Cells[2].Value = "1 ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "书名":
                        dataGridView1.Rows[i].Cells[1].Value = "200";
                        dataGridView1.Rows[i].Cells[2].Value = "1 ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "副题名":
                        dataGridView1.Rows[i].Cells[1].Value = "200";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "e";
                        break;
                    case "分辑号":
                        dataGridView1.Rows[i].Cells[1].Value = "200";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "h";
                        break;
                    case "责任者":
                        dataGridView1.Rows[i].Cells[1].Value = "200";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "f";
                        break;
                    case "分辑名":
                        dataGridView1.Rows[i].Cells[1].Value = "200";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "i";
                        break;
                    case "主要著作者":
                        dataGridView1.Rows[i].Cells[1].Value = "200";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "f";
                        break;
                    case "第一责任者":
                        dataGridView1.Rows[i].Cells[1].Value = "200";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "f";
                        break;
                    case "第二责任者":
                        dataGridView1.Rows[i].Cells[1].Value = "200";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "g";
                        break;
                    case "发行地":
                        dataGridView1.Rows[i].Cells[1].Value = "210";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "出版地":
                        dataGridView1.Rows[i].Cells[1].Value = "210";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "出版社":
                        dataGridView1.Rows[i].Cells[1].Value = "210";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "c";
                        break;
                    case "出版者":
                        dataGridView1.Rows[i].Cells[1].Value = "210";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "c";
                        break;
                    case "出版日期":
                        dataGridView1.Rows[i].Cells[1].Value = "210";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "d";
                        break;
                    case "出版时间":
                        dataGridView1.Rows[i].Cells[1].Value = "210";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "d";
                        break;
                    case "页码":
                        dataGridView1.Rows[i].Cells[1].Value = "215";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "丛书项":
                        dataGridView1.Rows[i].Cells[1].Value = "225";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "内容简介":
                        dataGridView1.Rows[i].Cells[1].Value = "330";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "一般性附注":
                        dataGridView1.Rows[i].Cells[1].Value = "300";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "读者对象":
                        dataGridView1.Rows[i].Cells[1].Value = "300";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "开本":
                        dataGridView1.Rows[i].Cells[1].Value = "215";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "d";
                        break;
                    case "图表":
                        dataGridView1.Rows[i].Cells[1].Value = "215";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "c";
                        break;
                    case "版本":
                        dataGridView1.Rows[i].Cells[1].Value = "205";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "作者简介":
                        dataGridView1.Rows[i].Cells[1].Value = "314";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "主题词":
                        dataGridView1.Rows[i].Cells[1].Value = "606";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "L606X":
                        dataGridView1.Rows[i].Cells[1].Value = "606";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "x";
                        break;
                    case "L606J":
                        dataGridView1.Rows[i].Cells[1].Value = "606";
                        dataGridView1.Rows[i].Cells[2].Value = "";
                        dataGridView1.Rows[i].Cells[3].Value = "j";
                        break;
                    case "分类":
                        dataGridView1.Rows[i].Cells[1].Value = "690";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "中图法分类":
                        dataGridView1.Rows[i].Cells[1].Value = "690";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "a";
                        break;
                    case "订数":
                        dataGridView1.Rows[i].Cells[1].Value = "905";
                        dataGridView1.Rows[i].Cells[2].Value = "  ";
                        dataGridView1.Rows[i].Cells[3].Value = "f";
                        break;
                }
            }
            this.re();
        }
        private void re()
        {
            DataGridViewRow dgvr = this.excelGridView.CurrentRow;
            //DataRowView current = this.BookData.Current as DataRowView;
            if (this.dataGridView1.Rows.Count > 0)
            {
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    if (string.IsNullOrEmpty((string)dataGridView1.Rows[i].Cells[1].Value))
                    {
                        dataGridView1.Rows.Remove(dataGridView1.Rows[i]);
                        i--;
                    }
                    //MessageBox.Show("nam0" + '\x001e'.ToString()+(string)dataGridView1.Rows[i].Cells[1].Value+ '\x001f'.ToString()+ (string)dataGridView1.Rows[i].Cells[2].Value);
                }
            }
            if (this.dataGridView1.Rows.Count > 0)
            {
                dt.Clear();
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty((string)dataGridView1.Rows[i].Cells[1].Value))
                    {
                        dt.Rows.Add();
                        dt.Rows[i]["字段标识"] = (string)dataGridView1.Rows[i].Cells[1].Value;
                        dt.Rows[i]["指"] = (string)dataGridView1.Rows[i].Cells[2].Value;
                        string lm = (string)dataGridView1.Rows[i].Cells[0].Value;
                        if (!string.IsNullOrEmpty((string)dataGridView1.Rows[i].Cells[3].Value))
                        {
                            string zzd = (string)dataGridView1.Rows[i].Cells[3].Value;
                            if (!string.IsNullOrEmpty(dgvr.Cells.GetCellValueFromColumnHeader(lm).ToString()))
                            {
                                string nr = dgvr.Cells.GetCellValueFromColumnHeader(lm).ToString();
                                dt.Rows[i]["子字段"] = '\x001f'.ToString() + zzd + nr;
                            }
                        }
                    }
                }
            }
            tabTemp.Clear();
            tabTemp.Dispose();
            FindComValue();
            if (tabTemp.Rows.Count > 0)
            {
                //string id = (BookLocal.GetGcMaxId("主键码", strDBKey) + Convert.ToInt32(current["主键码"].ToString().Trim())).ToString();
                string marc = "nam0" + '\x001e'.ToString() + "001" + "1".PadLeft(2, '0') + DateTime.Now.ToString("yyyy") + "1".PadLeft(6, '0') + '\x001e'.ToString() + "005" + DateTime.Now.ToString("yyyyMMddHHmmss.ff");
                for (int i = 0; i < tabTemp.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty(tabTemp.Rows[i]["子字段"].ToString()))
                    {
                        string zsf = tabTemp.Rows[i]["指"].ToString();
                        if (string.IsNullOrEmpty(zsf))
                            zsf = "  ";
                        marc += '\x001e'.ToString() + tabTemp.Rows[i]["字段标识"].ToString() + zsf + tabTemp.Rows[i]["子字段"].ToString();
                    }
                }
                this.marcGridHS1.MarcType = 1;
                this.marcGridHS1.MarcText = marc;
                this.marcGridHS1.SetGridView();
            }
        }
        
        private void FindComValue()
        {
            if (dt.Rows.Count > 0)
            {
                string row1 = dt.Rows[0][0].ToString();//取表的第一行第一列
                DataRow[] drr = dt.Select("字段标识=" + row1);

                DataRow drTemp = tabTemp.NewRow();//临时新的行
                foreach (DataRow row in drr)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        if (i != 0)
                        {
                            if (row[i].ToString().Trim() != "")
                                drTemp[i] = drTemp[i].ToString() + row[i].ToString();//合并每一列的数据
                        }
                        else
                        {
                            drTemp[i] = row[i].ToString();//添加第一列，不需要相加
                        }
                    }
                    dt.Rows.Remove(row);//清除此类的行
                }
                tabTemp.Rows.Add(drTemp);//添加临时新的行数据
                FindComValue();
            }
        }

        private void gshBtn_Click(object sender, EventArgs e)
        {
            string sbt = this.sBox.Text;
            int index = this.ebtBox.SelectedIndex;
            string bt = this.ebtBox.Items[index].ToString();
            this.excelGridView.Columns[bt].HeaderText = sbt;
            this.excelGridView.Columns[bt].DataPropertyName = bt;
            this.ebtBox.Items[index] = sbt;
            this.RefMarc();
        }

        private void excel_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.Rows.Count > 0)
            {
                dt.Clear();
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty((string)dataGridView1.Rows[i].Cells[1].Value))
                    {
                        DataGridViewRow dgvr = this.excelGridView.CurrentRow;
                        dt.Rows.Add();
                        dt.Rows[i]["字段标识"] = (string)dataGridView1.Rows[i].Cells[1].Value;
                        dt.Rows[i]["指"] = (string)dataGridView1.Rows[i].Cells[2].Value;
                        string lm = (string)dataGridView1.Rows[i].Cells[0].Value;
                        string nr;
                        if (!string.IsNullOrEmpty((string)dataGridView1.Rows[i].Cells[3].Value) && !string.IsNullOrEmpty(dgvr.Cells.GetCellValueFromColumnHeader(lm).ToString()))
                        {
                            string zzd = (string)dataGridView1.Rows[i].Cells[3].Value;
                            string znr = dgvr.Cells.GetCellValueFromColumnHeader(lm).ToString();
                            nr = '\x001f'.ToString() + zzd + znr;
                        }
                        else nr = "";
                        dt.Rows[i]["子字段"] = nr;
                    }
                }
            }
            tabTemp.Clear();
            tabTemp.Dispose();
            FindComValue();
            if (tabTemp.Rows.Count > 0)
            {
                //string id = (BookLocal.GetGcMaxId("主键码", strDBKey) + Convert.ToInt32(current["主键码"].ToString().Trim())).ToString();
                this.marcText = "nam0" + '\x001e'.ToString() + "001" + "1".PadLeft(2, '0') + DateTime.Now.ToString("yyyy") + "1".PadLeft(6, '0') + '\x001e'.ToString() + "005" + DateTime.Now.ToString("yyyyMMddHHmmss.ff");
                for (int i = 0; i < tabTemp.Rows.Count; i++)
                {
                    string zsf = tabTemp.Rows[i]["指"].ToString();
                    if (string.IsNullOrEmpty(zsf))
                        zsf = "  ";
                    if (!string.IsNullOrEmpty(tabTemp.Rows[i]["子字段"].ToString()))
                        this.marcText += '\x001e'.ToString() + tabTemp.Rows[i]["字段标识"].ToString() + zsf + tabTemp.Rows[i]["子字段"].ToString();
                }
                this.marcGridHS1.MarcType = 1;
                this.marcGridHS1.MarcText = this.marcText;
                this.marcGridHS1.SetGridView();
            }
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            FrmCxEiso eiso = new FrmCxEiso();
            if (eiso.ShowDialog() == DialogResult.OK)
            {
                string zdh = eiso.zdh;
                int fxsjm = eiso.fxsjm;
                if (MessageBox.Show("请确认是否导入到选书库？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    if (this.excelGridView.Rows.Count > 0)
                    {
                        using (DbOperHandler _open = new Common().Doh())
                        {
                            FrmWait wait = new FrmWait();
                            wait.Show();
                            for (int i = 0; i < this.excelGridView.Rows.Count; i++)
                            {
                                wait.label4.Text = "正在导入数据 " + (i+1).ToString();
                                Application.DoEvents();
                                //this.excelGridView.FirstDisplayedScrollingRowIndex = i;
                                this.excelGridView.CurrentCell = this.excelGridView.Rows[i].Cells[0];
                                //模拟点击事件（点击选中行）
                                this.excel_CellClick(excelGridView, new DataGridViewCellEventArgs(excelGridView.SelectedCells[0].ColumnIndex, excelGridView.SelectedCells[0].RowIndex));
                                //Console.WriteLine(this.marcText);
                                Books b = GetBooks.SetBooks(this.marcText);
                                object[,] value = new object[17, 2] {
                                    {"库键码",kjm},
                                    {"题名",StringUtil.GetStrLength(b.Ztitle)>100?StringUtil.SubStrByte(b.Ztitle,0,100):b.Ztitle},
                                    {"责任者",StringUtil.GetStrLength(b.Author)>50?StringUtil.SubStrByte(b.Author,0,50):b.Author},
                                    {"出版者",StringUtil.GetStrLength(b.Press)>50?StringUtil.SubStrByte(b.Press,0,50):b.Press},
                                    {"出版年",StringUtil.GetStrLength(b.Pdate)>20?StringUtil.SubStrByte(b.Pdate,0,20):b.Pdate},
                                    {"标准编码",b.Sisbn},
                                    {"入库日期",DateTime.Now},
                                    {"单价",StringUtil.GetStrLength(b.Ydj)>16?StringUtil.SubStrByte(b.Ydj,0,16):b.Ydj},
                                    {"订价",b.Sdj},
                                    {"分类",b.Ssh},
                                    {"订数",b.Count},
                                    {"操作员",this.uid},
                                    {"marc",this.marcText},
                                    {"征订号",zdh},
                                    {"开始时间",eiso.stime},
                                    {"结束时间",eiso.etime},
                                    {"图书来源",eiso.ly}
                                };
                                _open.Reset();
                                _open.AddFieldItems(value);
                                _open.Insert("待采书库");
                            }
                            object[,] value1 = new object[6, 2] {
                                {"发行商键码",fxsjm},
                                {"征订单号",zdh},
                                {"开始时间",eiso.stime},
                                {"结束时间",eiso.etime},
                                {"库键码",this.kjm},
                                {"馆键码",1}
                            };
                            _open.Reset();
                            _open.AddFieldItems(value1);
                            _open.Insert("选书征订单");
                            this.excelGridView.DataSource = null;
                            wait.Close();
                        }
                    }
                }
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            base.Close();
            string formName = "FrmCgxs";
            Form fr = System.Windows.Forms.Application.OpenForms[formName];
            if (fr != null)
            {
                FrmCgxs f1 = (FrmCgxs)fr;
                f1.SetTree();
            }
        }
    }
}
